package cn.edu.pzhu.service.imp;


import cn.edu.pzhu.dao.UserInfoDAO;
import cn.edu.pzhu.dao.imp.UserInfoDAOImp;
import cn.edu.pzhu.pojo.Msg;
import cn.edu.pzhu.pojo.UserInfo;
import cn.edu.pzhu.service.UserInfoService;
import cn.edu.pzhu.util.EmailUtil;

public class UserInfoServiceImp implements UserInfoService{

	private UserInfoDAO dao = new UserInfoDAOImp();
	@Override
	public Msg registInfo(UserInfo userinfo) {
		int n = dao.insert(userinfo);
		if(n>0) {
			return new Msg(true, "个人信息写入成功!");
		}
		return new Msg(false,"个人信息写入失败!");
	}

	@Override
	public UserInfo showUserInfo(String username) {
		UserInfo dbUserInfo = dao.selectById(username);
		if (dbUserInfo == null) {
			return null;
		}
		return dbUserInfo;
	}

	@Override
	public Msg modUserInfo(UserInfo userInfo) {
		int n = dao.update(userInfo.getUsername(), userInfo);
		if (n > 0) {
			return new Msg(true,"更新用户信息成功");
		}
		return new Msg(false,"更新用户信息失败");
	}

	@Override
	public Msg sendEmail(String username, String email) {
		//数据校验
		UserInfo userInfo = dao.selectById(username);
		if (userInfo == null) {
			return new Msg(false, "此用户不存在！");
		}
		//还需要查询user表，检查user是否状态为0  //此用户已经被注销
		if(!userInfo.getEmail().equals(email)) {
			return new Msg(false, "邮箱与注册时不相符！");
		}
		//发送邮件
		return EmailUtil.sendEmail(email);
	}

	@Override
	public Msg deleteUserInfo(String username) {
		int n = dao.destroy(username);
		if (n > 0) {
			return new Msg(true,"个人信息删除成功");
		}
		return new Msg(false,"个人信息删除失败");
	}
	
}
